package com.daohang361.imqq.protocol;

import java.nio.charset.Charset;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import com.daohang361.imqq.Asycore.Connector;
import com.daohang361.imqq.Asycore.MyLoger;
import com.daohang361.imqq.Asycore.QQUser;
import com.daohang361.imqq.decoder.DefaultDecoder;
import com.daohang361.imqq.decoder.InterfaceDecoder;
import com.daohang361.imqq.decoder.LoginDecoder;
import com.daohang361.imqq.hotLoader.PuginManager;
import com.daohang361.imqq.ServerContex.ServerContex;
import com.daohang361.imqq.ServerContex.SessionContex;
import com.daohang361.imqq.tool.Hex2Bytes;
import com.daohang361.imqq.tool.MyByteReciveArray;
import com.daohang361.imqq.tool.MyUtil;
/**
 * 此类用于包解密,底层数据转换,严格将不应该做任何业务逻辑控制
 * mina 解码控制端
 */
public class HexDecodeProtocol extends CumulativeProtocolDecoder {
	private  Charset charset;
	public HexDecodeProtocol(Charset defaultCharset) {
		this.charset=defaultCharset;
	}

	public Charset getCharset() {
		return charset;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}

	@Override
	protected boolean doDecode(IoSession session, IoBuffer in,
			ProtocolDecoderOutput out) throws Exception {
		System.out.println("decode:");
		byte[] allbyte=Hex2Bytes.hexStringToBytes(in.getHexDump());
		MyLoger.printByteToHex(in.getHexDump());
		in.position(in.limit());

		String address=session.getRemoteAddress().toString();
		System.out.println("address:"+address);

		MyByteReciveArray myArray = new MyByteReciveArray();
		myArray.init(allbyte);
		myArray.extractHead();//提取版本信息QQ号码等固定内容
		myArray.deleteTail();//去掉包尾
		QQUser sessionQQ =SessionContex.getQQUser(myArray.getQq());//从session中获取会话QQ属性
		sessionQQ.setRecive();
		System.out.println("From QQ:"+sessionQQ.getQq()+"接收第:"+sessionQQ.getRecive()+"包");
		Connector sessionConnector = SessionContex.getConnector(myArray.getQq());
		if(sessionQQ.getLogintimes()==-1){
			return true;
			}
		else if(sessionQQ.getLogintimes()<6){//基本登陆步凑
				sessionQQ=new LoginDecoder(myArray,sessionQQ).decode();
				byte[] newIp=sessionQQ.getAttr("redirectIp");
            	if(newIp!=null)
            		{
            		System.out.println("MyUtil.bytes2IP(newIp):"+MyUtil.bytes2IP(newIp));
            		sessionConnector.setServer_ip( MyUtil.bytes2IP(newIp));
            		}
		}else{
				System.out.println("decode loginTimes:"+sessionQQ.getLogintimes()+". command: "+Integer.toHexString(myArray.getCommand()));
				InterfaceDecoder decoder=Myproper.getDecoder(myArray.getCommand());
				decoder=decoder==null?(InterfaceDecoder)PuginManager.GetPuginByKey("Decoder"+MyUtil.intToHex(myArray.getCommand())):decoder;
				if(decoder==null)
					decoder=new DefaultDecoder();
				decoder.init(myArray, sessionQQ);
				sessionQQ=decoder.decode();
				if(myArray.getCommand()==ServerContex.getNowCommand(ServerContex.getSize()-1)){
					sessionQQ.setLogin(true);//设置登陆成功
					System.out.println("[decoder protocol]反馈登陆成功");
				}
		}
		if(sessionQQ==null)
			return true;
		out.write(sessionQQ);
		return true;
	}
}
